﻿// Decompiled with JetBrains decompiler
// Type: NetickEditor.NetickEditorUtils
// Assembly: Netick.Unity.Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 0658BB4A-05C4-428D-9BD6-82E94959F5A1
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Editor\Netick.Unity.Editor.dll

using Netick.Unity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;

#nullable disable
namespace NetickEditor;

internal static class NetickEditorUtils
{
  private static HashSet<int> _hashIds = new HashSet<int>(500);

  internal static NetickConfig GetConfig(bool createIfNotExist = false)
  {
    NetickConfig config = (NetickConfig) AssetDatabase.LoadAssetAtPath("Assets/Netick/Resources/netickConfig.asset", typeof (NetickConfig));
    if ((UnityEngine.Object) config == (UnityEngine.Object) null & createIfNotExist)
    {
      if (!AssetDatabase.IsValidFolder("Assets/Netick"))
        AssetDatabase.CreateFolder("Assets", "Netick");
      if (!AssetDatabase.IsValidFolder("Assets/Netick/Resources"))
        AssetDatabase.CreateFolder("Assets/Netick", "Resources");
      config = ScriptableObject.CreateInstance<NetickConfig>();
      string str1 = "Assets/Netick/Resources/netickConfigDefault.asset";
      string str2 = "Assets/Netick/Resources/netickConfig.asset";
      if ((bool) AssetDatabase.LoadAssetAtPath(str1, typeof (NetickConfig)))
        AssetDatabase.CopyAsset(str1, str2);
      else
        AssetDatabase.CreateAsset((UnityEngine.Object) config, str2);
      if (config.AutoGetPrefabs)
        NetickPrefabTools.LoadAllNetworkedPrefabs(config);
      AssetDatabase.SaveAssets();
    }
    return config;
  }

  public static void ConfigureNetworkSceneObjects()
  {
    foreach (NetworkAnimator networkAnimator in UnityEngine.Object.FindObjectsOfType<NetworkAnimator>(true))
      NetickEditorAnimationUtils.SetupAnimator(networkAnimator);
    List<NetworkObject> list = ((IEnumerable<NetworkObject>) UnityEngine.Object.FindObjectsByType<NetworkObject>(FindObjectsInactive.Include, FindObjectsSortMode.InstanceID)).ToList<NetworkObject>();
    list.Sort((Comparison<NetworkObject>) ((x, y) => y.GetInstanceID().CompareTo(x.GetInstanceID())));
    NetickEditorUtils._hashIds.Clear();
    foreach (NetworkObject networkObject in list)
    {
      bool flag = false;
      if (NetickEditorUtils._hashIds.Contains(networkObject._SceneId))
      {
        flag = true;
        networkObject._SceneId = -1;
      }
      if (networkObject._SceneId != -1)
        NetickEditorUtils._hashIds.Add(networkObject._SceneId);
      if (flag)
      {
        new SerializedObject((UnityEngine.Object) networkObject).ApplyModifiedProperties();
        EditorUtility.SetDirty((UnityEngine.Object) networkObject);
      }
    }
    foreach (NetworkObject sceneObj in list)
    {
      int num = NetickEditorUtils.ConfigureNetworkSceneObject(sceneObj);
      NetickEditorUtils._hashIds.Add(num);
    }
  }

  public static int ConfigureNetworkSceneObject(NetworkObject sceneObj)
  {
    bool flag = false;
    if (sceneObj._SceneId == -1)
    {
      int num = sceneObj._SceneId;
      while (num == -1 || NetickEditorUtils._hashIds.Contains(num))
        num = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
      sceneObj._SceneId = num;
      flag = true;
    }
    NetickEditorUtils._hashIds.Add(sceneObj._SceneId);
    sceneObj.PrefabId = -1;
    sceneObj.PrefabIndex = -1;
    sceneObj.PrefabRoot = (NetworkObject) null;
    sceneObj.Children = new NetworkObject[0];
    if (flag)
    {
      new SerializedObject((UnityEngine.Object) sceneObj).ApplyModifiedProperties();
      EditorUtility.SetDirty((UnityEngine.Object) sceneObj);
    }
    return sceneObj._SceneId;
  }

  public static bool IsSubPathOf(this string path, string baseDirPath)
  {
    return NetickEditorUtils.WithEnding(Path.GetFullPath(path.Replace('/', '\\')), "\\").StartsWith(NetickEditorUtils.WithEnding(Path.GetFullPath(baseDirPath.Replace('/', '\\')), "\\"), StringComparison.OrdinalIgnoreCase);
  }

  private static string WithEnding(string str, string ending)
  {
    if (str == null)
      return ending;
    string str1 = str;
    for (int length = 0; length <= ending.Length; ++length)
    {
      string str2 = str1 + NetickEditorUtils.Right(ending, length);
      if (str2.EndsWith(ending))
        return str2;
    }
    return str1;
  }

  private static string Right(string value, int length)
  {
    if (value == null)
      throw new ArgumentNullException(nameof (value));
    if (length < 0)
      throw new ArgumentOutOfRangeException(nameof (length), (object) length, "Length is less than zero");
    return length >= value.Length ? value : value.Substring(value.Length - length);
  }

  internal static bool ShouldRecompile()
  {
    foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
      if (((IEnumerable<AssemblyName>) assembly.GetReferencedAssemblies()).FirstOrDefault<AssemblyName>((Func<AssemblyName, bool>) (x => x.Name.Contains("Netick"))) != null && ((IEnumerable<System.Type>) assembly.GetTypes()).ToList<System.Type>().Find((Predicate<System.Type>) (x => x.Name == "___ModifiedByNetick")) == (System.Type) null)
        return true;
    }
    return false;
  }

  public static bool IsBeingEditedInIsolatedPrefabMode(this GameObject obj)
  {
    return (UnityEngine.Object) PrefabStageUtility.GetPrefabStage(obj) != (UnityEngine.Object) null;
  }

  public static bool IsUninstantiatedPrefab(GameObject prefabInstance)
  {
    return PrefabUtility.GetPrefabAssetType((UnityEngine.Object) prefabInstance) != null && PrefabUtility.GetPrefabInstanceStatus((UnityEngine.Object) prefabInstance) == 0;
  }
}
